package dev.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * @author Mr.C
 *
 */
public class DateUtil {

	/**
	 * 格式化输出 yyyy-MM-dd HH:mm:ss Mr.C 2017年5月8日
	 * 
	 * @param date
	 * @return
	 */
	public static String dateStr(Date date) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = format.format(date);
		return dateStr;
	}

	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		return year;
	}

	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);
		return month+1;
	}
	
	public static int getWeekOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int i = calendar.get(Calendar.WEEK_OF_MONTH);
		return i;
	}

	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int i = calendar.get(Calendar.HOUR_OF_DAY);
		return i;
	}
	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int i = calendar.get(Calendar.MINUTE);
		return i;
	}
	/**
	 * 
	 * @param date
	 * @param dayType
	 *            Calendar.DAY_OF_MONTH
	 * @return
	 */
	public static int getDayofMonth(Date date, int dayType) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int day = calendar.get(dayType);
		return day;
	}

	/**
	 * 
	 * @param dateStr
	 *            2017-06-10 13:29:00
	 * @return 6月10日 13:29:00
	 */
	public static String formatter(String dateStr) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		try {
			date = format.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new SimpleDateFormat("MM月dd日 HH:mm:ss").format(date);
	}

	/**
	 * 
	 * @param date
	 * @return yyyy年MM月dd日 HH:mm:ss
	 */
	public static String fommatter(Date date) {
		DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		String dateStr = format.format(date);
		return dateStr;
	}

	/**
	 * 
	 * @param cms
	 *            CMS 中央时区时间 Wed Jul 05 23:17:40 CST 2017
	 * @return
	 */
	public static Date parse(String cms) {
		DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CST' yyyy", Locale.US);
		Date date = new Date();
		try {
			date = df.parse(cms);
		} catch (ParseException e) {
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.0");
			try {
				date = format.parse(cms);
			} catch (ParseException e1) {

			}
		}
		return date;
	}

	/**
	 * 日期字符串格式化为时间
	 * 
	 * @author chenjy
	 * @time 2018年1月10日 下午12:41:03
	 * @param dateStr
	 *            日期字符串
	 * @param pattern
	 *            格式化规则
	 * @return Date|null
	 */
	public static Date parse(String dateStr, String pattern) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		try {
			return simpleDateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 日期格式化 yyyy-MM-dd HH:mm:ss
	 * 
	 * @author chenjy
	 * @time 2018年1月10日 下午12:45:15
	 * @param dateStr
	 * @return Date|null
	 */
	public static Date parse2(String dateStr) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return simpleDateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	 
}